package com.wjb.xietong.ota.task;

import java.util.ArrayList;
import java.util.List;
import com.wjb.xietong.R;
import com.wjb.xietong.component.AppGlobal;
import com.wjb.xietong.server.taskdetail.GetTaskDetailResponse.GetTaskDetailNotifyInfo;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class NotifyPersonPopWindow extends PopupWindow {
	private PopupWindow pop = null;
	private Context mContext = null;
	private NotifyPersonResultListener listener;
	private NotifyPersonAdapter notifyPersonAdapter = null;
	private List<GetTaskDetailNotifyInfo> notifyInfoList = new ArrayList<GetTaskDetailNotifyInfo>();

	public NotifyPersonPopWindow(Context context) {
		this.mContext = context;
		initView();
	}
	
	public interface NotifyPersonResultListener{
		public void onResult(int index);
	} 
	
	public void showPopWindow(View v, NotifyPersonResultListener listener, List<GetTaskDetailNotifyInfo> notifyInfoList){
		this.listener = listener;
		this.notifyInfoList = notifyInfoList;
//		pop.showAtLocation(v.findViewById(R.id.contentLayout),
//				Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
//				0); // 设置layout在PopupWindow中显示的位置
		pop.showAsDropDown(v, 0, -v.getHeight() - (int)(AppGlobal.getInstance().getScreenDensity() * 150));
	}

	private void initView() {
		LinearLayout layout = (LinearLayout) LayoutInflater.from(mContext)
				.inflate(R.layout.notify_popmenu, null);
		// 创建PopupWindow对象
		pop = new PopupWindow(layout, (int)(AppGlobal.getInstance().getScreenDensity() * 180),
				(int)(AppGlobal.getInstance().getScreenDensity() * 150), false);
		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());
		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);
		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);
		pop.update();
		ListView listView = (ListView) layout.findViewById(R.id.listView);
		notifyPersonAdapter = new NotifyPersonAdapter(mContext);
		listView.setAdapter(notifyPersonAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if(null != listener){
					listener.onResult(arg2);
				}
			}
			
		});
	}

	/**
	 * 通知联系人列表的adapter
	 */
	private class NotifyPersonAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public NotifyPersonAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			if (null == notifyInfoList) {
				return 0;
			} else {
				return notifyInfoList.size();
			}
		}

		@Override
		public Object getItem(int arg0) {
			if (null != notifyInfoList) {
				return notifyInfoList.get(arg0);
			}
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			NotifyPersonHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.item_notify_person_list, null);
				holder = new NotifyPersonHolder();
				/* 得到各个控件的对象 */
				holder.tv_user_name = (TextView) convertView
						.findViewById(R.id.tv_user_name);
				convertView.setTag(holder); // 绑定ViewHolder对象
			} else {
				holder = (NotifyPersonHolder) convertView.getTag(); // 取出ViewHolder对象
			}
			holder.tv_user_name.setText(""
					+ notifyInfoList.get(position).getUsername());
			return convertView;
		}

		public final class NotifyPersonHolder {
			public TextView tv_user_name;
		}
	}
}
